package com.lanou3g.hw0420.copy2;

import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;

/**
 * Created by dllo on 18/4/21.
 */
public class EchoThread extends Thread {
  private Socket client;
    public EchoThread(Socket client){
        this.client=client;
    }

    @Override
    public void run() {
        BufferedReader reader=null;
        PrintWriter pw =null;
        try {
            OutputStream os =client.getOutputStream();
            InputStream is =client.getInputStream();
            reader=new BufferedReader(new InputStreamReader(is));
            pw= new PrintWriter(new OutputStreamWriter(os));
            String line="";
            while ((line=reader.readLine())!=null){
                String echo="ECHO:"+line;
                pw.println(echo);
                pw.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (reader!=null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }if (pw!=null){
                pw.close();
            }
            if (client!=null){
                try {
                    client.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
